<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/27
 * Time: 14:43
 */
namespace app\admin\model;

class Room extends DataBase
{
    protected $name = 'Rooms';
    protected $autoWriteTimestamp = true;
    protected $createTime = 'created_at';
    protected $updateTime = 'updated_at';

    /**
     * Notes: 房间流水数据
     * User: 王俊成
     * Date: 2018/10/31
     * Time: 16:59
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function roomFlowingData(){
        $map = ['r_id'=>'user_id'];
        $page_config = array(
            'page' => input('pageNum', 1),
            'list_rows' => input('limit', 10),
        );
        $keyWords = input('get.keyWords');
        $map = [];
        if(!empty($keyWords) || $keyWords == '0'){
            $key = input('get.key');
            if($key == 'r_id')
                $map[] = ['user_id','=',$keyWords];

            if($key == 'u_id'){
                $user_id = User::where('u_number','=',$keyWords)->value('user_id');
                $map[] = ['user_id','=',$user_id];
            }

            if($key == 'r_name')
                $map[] = ['r_name','like','%'.$keyWords.'%'];

            if($key == 'nickname'){
                $user_id = User::where('nickname','=',$keyWords)->value('user_id');
                $map[] = ['user_id','=',$user_id];
            }
        }

        $data = $this->field('id,user_id as r_id,r_name')->where($map)->paginate($page_config);

        foreach($data as $key => $value){

            $userInfo = User::where('user_id','=',$value['r_id'])->find();
            $data[$key]['u_id'] = $userInfo['u_number'];
            $data[$key]['nickname'] = $userInfo['nickname'];
            $data[$key]['flowing_url'] =  'http://user.quniuyuyin.com/index.php/public/roomFlowing/id/'.encode($value['id']);
            $data[$key]['week_flowing'] =  'http://user.quniuyuyin.com/index.php/Manage/roomData/user_id/'.encode($userInfo['user_id']);
        }
        return $data;
    }

    /**
     * Notes: 某个房间的月流水数据
     * User: 王俊成
     * Date: 2018/10/31
     * Time: 17:58
     * @return array
     */
    public function roomFlowingInfoData(){
        $roomId = input('get.roomId');
        $queryTime = input('get.queryTime');
        if(!empty($queryTime) && $queryTime != date('Y-m')){
            if(strtotime($queryTime) > time()){
                return ['resultCode'=>110,'resultData'=>null,'resultDes'=>'查询结果集为空！'];
            }
            else{
                $startTime = strtotime($queryTime.'-01');
                $endTime = strtotime(date('Y-m-d',strtotime("$queryTime + 1 month - 1 day")).' 23:59:59');
            }
        }
        else{
            $startTime = strtotime(date('Y-m').'-01');
            $endTime = time();
        }
        $dayNum = ceil(($endTime - $startTime) / (3600 * 24));
        $data = [];
        for($i = 0;$i < $dayNum;$i++){
            $arr['date'] = date('Y-m-d',$startTime + 3600 * 24 * $i);
            $arr['diamonds'] = GiftRecord::where([['room_id','=',$roomId],['created_at','between',[strtotime($arr['date']),strtotime($arr['date'].' 23:59:59')]]])->sum('gift_price');
            $data[] = $arr;
        }
        return ['resultCode'=>100,'resultData'=>$data,'resultDes'=>'查询成功！'];

    }

    /**
     * Notes: 获取房间某天礼物流水详情
     * User: 王俊成
     * Date: 2018/11/1
     * Time: 11:08
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function roomGiftInfo(){
        $pageConfig = [
            'list_rows' => 25,
            'page' => input('get.page',1),
            'query' => [
                'roomId' => input('get.roomId'),
                'dates' => input('get.dates')
            ]
        ];
        $data =  GiftRecord::where([['room_id','=',input('get.roomId')],['create_time','between',[strtotime(input('get.dates')),strtotime(input('get.dates').' 23:59:59')]]])
            ->field('send_user_id,receive_user_id,gift_type,gift_price,num,create_time')->order('create_time DESC')->paginate($pageConfig);
        return $data;
    }

    /**
     * Notes: 获取房间某天小时级流水
     * User: 王俊成
     * Date: 2018/11/1
     * Time: 13:38
     * @return array
     */
    public function roomFlowingHour(){
        $times = strtotime(input('get.dates'));
        $data = [];
        for($i = 0;$i < 24;$i++){
            $arr['hours'] = $i.':00~'.($i + 1).':00';
            $arr['hours'] = $i.':00~'.($i + 1).':00';
            $arr['hours'] = $i.':00~'.($i + 1).':00';
            $arr['diamonds'] = GiftRecord::where([['room_id','=',input('get.roomId')],['create_time','egt',$times + $i * 3600],['create_time','lt',$times + ($i + 1) * 3600]])->sum('gift_price');
            $data[] = $arr;
        }
        return $data;
    }

    /**
     * Notes: 查看上周排行
     * User: 王俊成
     * Date: 2018/11/1
     * Time: 14:01
     */
    public function roomFlowingLastweek(){
        $roomId = input('get.roomId');
        $pageConfig = [
            'list_rows' => 25,
            'page' => input('get.page',1),
            'query' => [
                'roomId' => $roomId
            ]
        ];
        $startTime = strtotime(date('Y-m-d',strtotime('last week')));
        $endTime = $startTime + 7 * 24 * 3600;
        $data = GiftRecord::where([['room_id','=',$roomId],['created_at','egt',$startTime],['created_at','lt',$endTime]])->fieldRaw('send_user_id,room_id,sum(gift_price) as wealth')
            ->group('send_user_id')->order('wealth DESC')->paginate($pageConfig);
        return $data;
    }

    /**
     * Notes: 获取房间用户
     * User: 王俊成
     * Date: 2018/11/1
     * Time: 15:51
     * @return array|\think\Paginator
     * @throws \think\exception\DbException
     */
    public function getRoomUser(){
        $roomId = input('get.roomId');
        $user_id = RoomMembers::where('room_id',$roomId)->column('user_id');
//        $data = User::alias('a')->where([['a.user_id','in',$user_id],['a.user_type','=',1]])->join('users_info as b','b.user_id = a.user_id')->field('a.user_id as id,a.u_number as u_id,a.nickname,a.head_portrait as pic,a.signature,a.sex,b.birthday')
        $data = User::alias('a')->where([['a.user_id','in',$user_id],['a.user_type','=',1]])->join('users_info','users_info.user_id = a.user_id')->field('a.user_id as id,a.u_number as u_id,a.nickname,a.head_portrait as pic,a.signature,a.sex,users_info.birthday')
            ->order('a.created_at DESC')->paginate(25);
        return $data;
    }


    public function  banDisplayData()
    {

        $temp = ['u_id'=>'u_number','r_id'=>'user_id'];
        $page_config = array(
            'page' => input('get.offset', 1),
            'list_rows' => input('get.limit', 10),
        );

        $map[] = ['a.room_type','eq',2];

        $data = $this
            ->alias('a')
            ->field('a.id,b.u_number as u_id,b.nickname,a.r_name')
            ->join('users b','a.user_id = b.user_id','left')
            ->where($map)
            ->paginate($page_config);

        foreach($data as $key => $value){
            $data[$key]['nickname'] = preg_replace_callback('/@E(.{6}==)/', function ($r) {
                return base64_decode($r[1]);
            }, $value['nickname']);

            $data[$key]['r_name'] = preg_replace_callback('/@E(.{6}==)/', function ($r) {
                return base64_decode($r[1]);
            }, $value['r_name']);
        }

        return $data;
    }
}